<?php
/**
 * goods 表的模型
 * @author 周英勇 15913362842@163.com
 */
class Goods_model extends CI_model
{
	public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    
    /**
 	* 获取首页banner信息列表
 	*根据创建时间倒序排列
 	*/
    public function getGoodsList($activity_id = null,$goods_name = null){
    	$this->db->select('goods_id,banner_img,name,other_name,descript,price,update_time,status');
        $this->db->from('goods');
        if(!empty($activity_id)){
            $this->db->where('activity_id', $activity_id);
        }
        if(!empty($goods_name)){
            $this->db->where('name like "%'.$goods_name.'%"');
        }
        $this->db->order_by('create_time desc,status');
        $query = $this->db->get();
        if ($query->num_rows() == 0) {
            return null;
        }
        return $query->result_array();
    }
    
    /**
    * 根据多个ID获取活动数据
    *根据status排序
    */
    public function getGoodsListByIds($ids){

        $this->db->select('goods_id,name,other_name,price,banner_img,status,activity_id');
        $this->db->from('goods');
        $this->db->where_in('goods_id',$ids);
        $this->db->order_by('status asc');
        $query = $this->db->get();
        if ($query->num_rows() == 0) {
            return null;
        }
        return $query->result_array();
    }
    public function getOnlineGoodsList($activity_id = null,$goods_name = null){
        $this->db->select('goods_id,banner_img,name,other_name,descript,price,update_time,status');
        $this->db->from('goods');
        if(!empty($activity_id)){
            $this->db->where('activity_id', $activity_id);
        }
        if(!empty($goods_name)){
            $this->db->where('name like "%'.$goods_name.'%"');
        }
        $this->db->where('status',2);
        $this->db->order_by('create_time desc,status');
        $query = $this->db->get();
        if ($query->num_rows() == 0) {
            return null;
        }
        return $query->result_array();
    }
    /**
 	* 根据goods_id获取一条记录
 	*/
    public  function  getGoodsById($id){
        $where['goods_id'] = $id;
   		$this->db->select('*');
        $this->db->from('goods');
        $this->db->where($where);
        $query = $this->db->get();
        if ($query->num_rows() == 0) {
            return null;
        }
        return $query->row_array();
    }
    
    /**
    * 往goods表中插入一条记录
    *params 参数
    */
    public  function addGoods($params){
        if(empty($params)){
            return 0;
        }
        $this->db->insert('goods', $params);
        return $this->db->insert_id();
       
    }
    
    /**
    * 根据id从goods表中更新一条记录
    * id
    * params 参数
    */
    public function updateGoodsById($id,$params){
        if(empty($id)){
            return 0;
        }
        $this->db->where('goods_id',$id); 
        $this->db->update('goods',$params);
        return $this->db->affected_rows();
    }

    /**
    * 根据id从goods表中删除一条记录
    * id
    */
    public function deleteGoodsById($id){
        if(empty($id)){
            return 0;
        }
        $this->db->where('goods_id','$id'); 
        $this->db->delete_id('goods',$params);
        return $this->db->affected_rows();
    }

    function getGoods(){
        $this->db->select("goods_id,repertory_id,name,price,status,create_time");
        $this->db->from('goods');
        $this->db->where('status', 2);
        $this->db->order_by('create_time desc');
        $query = $this->db->get();
        if ($query->num_rows() == 0) {
            return null;
        }
        return $query->result_array();
    }

}